<?php

if (!defined('IN_XLP')) {
    exit('Access Denied!');
}

/**
 * Description of userAction
 *
 * @author xlp
 */
class userAction extends commonAction {

    protected $adminConfig = array();
    protected $status = array(0 => '<span class="red">未审核</span>', 1 => '正常', 2 => '<span class="grey">无效</span>');

    function __construct() {
        parent::__construct();
        parent::_checkLogin();
        $this->adminConfig = C('admin');
    }

    function index() {
        $p = $this->_getid('p', 1);
        $group = $this->_getid('group', 0);
        $q = $this->_get('q');
        $where = 'a.city_id=' . admin::$adminInfo['city_id'];
        $countWehre = array('city_id' => admin::$adminInfo['city_id']);
        if ($group) {
            $where.=' AND a.groupid=' . ($group - 1);
            $countWehre['groupid'] = ($group - 1);
        }
        if ($q) {
            $where.=" AND a.username LIKE '%" . parent::safeSearch($q) . "%'";
            $countWehre['LIKE'] = array('username' => $q);
        }
        $pageShow = 20;
//        D('admin')->setShowError();
//        $rs = D('admin')->where($where)->page($p)->findAll();
        $rs = D('admin')->query("SELECT a.*,b.name AS areaName,c.title AS villageName FROM __TABLE__ AS a LEFT JOIN __PRE__area AS b ON a.aid=b.aid LEFT JOIN __PRE__village AS c ON a.village_id=c.vid WHERE " . $where . " ORDER BY a.id DESC LIMIT " . ($p - 1) * $pageShow . ',' . $pageShow);
        $total = D('admin')->where($countWehre)->count();
        $this->assign(array('rs' => $rs, 'status' => $this->status, 'group' => $this->adminConfig['group'], 'p' => $p, 'total' => $total, 'group_id' => $group));
        $this->display();
    }

    function add() {
        $this->assign(array('status' => $this->status, 'group' => $this->adminConfig['group']));
        $this->display();
    }

    function edit() {
        if (!parent::_checkIsAdmin()) {
            return $this->psw();
        }
        $id = $this->_getid('id', admin::$adminInfo['id']);
        if (!$id) {
            showError('参数丢失');
        }
        $rs = D('admin')->where(array('id' => $id))->find();
        if (!$rs) {
            showError('帐号不存在');
        }
        $rs['group_name'] = $rs['aid'] ? parent::getAreaName($rs['aid']) : '';
        $rs['village_name'] = $rs['village_id'] ? M('village')->where(array('vid' => $rs['village_id']))->getField('title') : '';
        $this->assign(array('rs' => $rs, 'status' => $this->status, 'group' => $this->adminConfig['group']));
        $this->display();
    }

    function post() {
//        z($_POST);
        $objData = array();
        //获取字段内容
        $fieldList = D('admin')->getTableFields();
        foreach ($fieldList['fields'] as $key => $val) {
            $objData[$key] = $val['type'] == 'int' ? $this->_postid($key, $val['value']) : $this->_post($key, $val['value']);
        }
        $objData['effective'] = $this->_post('effective', 0);
        $objData['regdateline'] = TIME;
        //内容规则检查
        T('content/validate');
        $validate = array(
            array('username', 'username', '帐户名称不符合要求', 2, 30),
            array('psw', 'min_length', '密码长度必须大于4位', 4),
        );
        if (!validate::check($validate, $objData)) {
            $this->JsonReturn(validate::getError());
        }
        if ($objData['psw'] != $this->_post('repsw')) {
            $this->JsonReturn('两次密码不一致，请检查');
        }
        if (D('admin')->field('id')->where(array('username' => $objData['username'], 'city_id' => admin::$adminInfo['city_id']))->find()) {
            $this->JsonReturn('该帐户名已存在');
        }
        $objData['psw'] = D('admin')->setUserPassword($objData['psw']);
        if ($objData['effective']) {
            $objData['effective'] = inTime($objData['effective']);
        } else {
            $objData['effective'] = 0;
        }
        $objData['status'] = 1;
        // if (($objData['groupid'] == 2 || $objData['groupid'] == 3) && !$objData['aid']) {
        //     $this->JsonReturn('请选择管理区域');
        // }
        // if ($objData['groupid'] == 4 && !$objData['village_id']) {
        //     $this->JsonReturn('请选择管理小区');
        // }
        // if ($objData['groupid'] == 4) {
        //     $objData['aid'] = M('village')->where(array('vid' => $objData['village_id']))->getField('community_id');
        //     if (!$objData['aid']) {
        //         $this->JsonReturn('该小区还未设置街道和社区');
        //     }
        // } else {//不是小区管理员则删除小区
        //     $objData['village_id'] = 0;
        // }
        if ($objData['groupid'] == 5 || $objData['groupid'] == 6) {//网站编辑、客服
            $objData['aid'] = 0;
        }
        $objData['city_id'] = admin::$adminInfo['city_id'];
//        z($objData);
        $id = D('admin')->insert($objData);
        $this->JsonReturn('ok', $id, 1);
    }

    function save() {
        $id = $this->_postid('id', 0);
        if (!$id) {
            $this->JsonReturn('参数丢失');
        }
        $objData = array();
        $noFields = array('logincount', 'loginip', 'logintime', 'regdateline');
        if (!parent::_checkIsAdmin()) {
            array_unshift($noFields, array('city_id', 'aid', 'effective', 'groupid', 'auth_id', 'village_id', 'status'));
        }
        //获取字段内容
        $fieldList = D('admin')->getTableFields($noFields);
        foreach ($fieldList['fields'] as $key => $val) {
            $objData[$key] = $val['type'] == 'int' ? $this->_postid($key, $val['value']) : $this->_post($key, $val['value']);
        }
        $objData['repsw'] = $this->_post('repsw');
        $rs = D('admin')->where(array('id' => $id))->find();
        if (!parent::_checkIsAdmin() && $rs['id'] != admin::$adminInfo['id']) {
            $this->JsonReturn('权限不足');
        }
        //内容规则检查
        T('content/validate');
        $validate = array(
            array('username', 'username', '帐户名称不符合要求', 2, 30),
        );
        if ($objData['psw']) {
            $validate[] = array('psw', 'min_length', '密码长度必须大于4位', 4);
        }
        if (!validate::check($validate, $objData)) {
            $this->JsonReturn(validate::getError());
        }
        if ($objData['psw'] && ($objData['psw'] != $objData['repsw'])) {
            $this->JsonReturn('两次密码不一致');
        }
        if ($objData['psw']) {
            $objData['psw'] = D('admin')->setUserPassword($objData['psw']);
        } else {
            unset($objData['psw']);
        }
        $checkAdmin = D('admin')->where(array('username' => $objData['username'], 'city_id' => admin::$adminInfo['city_id']))->getField('id');
        if ($checkAdmin && $checkAdmin != $id) {
            $this->JsonReturn('该帐户名已存在');
        }
        if (parent::_checkIsAdmin()) {
            $objData['effective'] = $this->_post('effective', 0);
            if ($objData['effective']) {
                $objData['effective'] = inTime($objData['effective']);
            } else {
                $objData['effective'] = 0;
            }
            $objData['status'] = $objData['status'] ? $objData['status'] : 0;
            if (($objData['groupid'] == 2 || $objData['groupid'] == 3) && !$objData['aid']) {
                $this->JsonReturn('请选择管理区域');
            }
            if ($objData['groupid'] == 4 && !$objData['village_id']) {
                $this->JsonReturn('请选择管理小区');
            }
            if ($objData['groupid'] == 4) {
                $objData['aid'] = M('village')->where(array('vid' => $objData['village_id']))->getField('community_id');
                if (!$objData['aid']) {
                    $this->JsonReturn('该小区还未设置街道和社区');
                }
            } else {//不是小区管理员则删除小区
                $objData['village_id'] = 0;
            }
            if ($objData['groupid'] == 5 || $objData['groupid'] == 6) {//网站编辑、客服
                $objData['aid'] = 0;
            }
        }
        $objData['city_id'] = admin::$adminInfo['city_id'];
        unset($objData['repsw']);
//        z($objData);
        D('admin')->update($objData, array('id' => $id));
        $this->JsonReturn('ok', null, 1);
    }

    function delete() {
        $id = $this->_postid('id');
        if (!$id) {
            showError('参数丢失');
        }
        $rs = D('admin')->where(array('id' => $id))->find();
        if ((!parent::_checkIsAdmin() && $rs['aid'] != admin::$adminInfo['aid'])) {
            showError('权限不足');
        }
        D('admin')->delAdmin(array('id' => $id));
        //DOTO:清除相关数据
        $this->JsonReturn('ok', null, 1);
    }

}
